package com.ctn.information.menu;

import com.ctn.information.MainActivity;
import com.ctn.information.R;
import com.ctn.information.util.SwitchFragmentUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 左边菜单栏View Item Fragment
 * 
 * @author HLJ
 *
 */
@SuppressLint("NewApi") public class MenuTopicListFragment extends ListFragment {

	/**
	 * 初始化View
	 */
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}
	
	/**
	 * 对应菜单栏目图标
	 */
	private int[] icon = new int[]{R.drawable.menu_news_icon,
								   R.drawable.menu_business_icon,
								   R.drawable.menu_dealer_icon,
								   R.drawable.menu_state_icon,
								   R.drawable.menu_weibo_icon,
								   R.drawable.menu_mobilenews_icon,
								   R.drawable.menu_topic_icon,
								   R.drawable.menu_class_icon,
								   R.drawable.menu_buy_icon,
								   R.drawable.menu_festival_icon,
								   R.drawable.menu_road_icon};
	
	
	/**
	 * 创建Activity
	 */
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		TopicAdapter adapter = new TopicAdapter(getActivity());
		String[] topic = getResources().getStringArray(R.array.topic_menu_names);
		String[] topicId = getResources().getStringArray(R.array.topic_menu_names_id);
		/** 绑定Fragment Class Name */
		SwitchFragmentUtil.bindTopocFragmentsIndex(getResources().getStringArray(R.array.menu_topic_content_view));
		/** 填充adapter */
		for (int i = 0; i < topic.length; i++) {
			TopicItem item = new TopicItem(topic[i],icon[i],topicId[i]);
			if(i==0)item.apha = 255;
			adapter.add(item);
		}
		setListAdapter(adapter);
	}

	/**
	 * 菜单栏Item
	 * 
	 * @author HLJ
	 *
	 */
	private class TopicItem {
		public String tag;
		public int iconRes;
		public int apha = 0;
		public String id;
		public TopicItem(String tag, int iconRes,String id) {
			this.tag = tag; 
			this.id = id;
			this.iconRes = iconRes;
		}
	}

	/**
	 * 菜单栏目装载适配器
	 * 
	 * @author HLJ
	 *
	 */
	public class TopicAdapter extends ArrayAdapter<TopicItem> {

		public TopicAdapter(Context context) {
			super(context, 0);
		}

		@SuppressLint("ResourceAsColor") public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
			}
			
			ImageView indicatorIcon = (ImageView) convertView.findViewById(R.id.row_indicator_icon);
			if (getItem(position).apha == 255) {
				indicatorIcon.setVisibility(View.VISIBLE);
			}else{
				indicatorIcon.setVisibility(View.INVISIBLE);
			}
			
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			icon.setBackgroundColor(R.color.menu_topic_bg);
			icon.getBackground().setAlpha(getItem(position).apha);
			
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);
			title.setBackgroundColor(R.color.menu_topic_bg);
			title.getBackground().setAlpha(getItem(position).apha);
			
			return convertView;
		}

	}

	/**
	 * 切换面板准备，获取activity
	 * 
	 * @param pos
	 * @param main
	 */
	private void switchFragment(Fragment fragment,MainActivity main) {
		if (main == null)
			return;
		main.switchContent(fragment);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		/** 更改menu 展示风格（选中时） */
		TopicAdapter ta = (TopicAdapter)l.getAdapter();
		int size = ta.getCount();
		/** 先取消上一次选中时的menu的风格 */
		for (int i = 0; i < size; i++) {
			ta.getItem(i).apha = 0;
		}
		/** 设置现在选中的menu风格 */
		ta.getItem(position).apha = 255;
		String subTitle = ta.getItem(position).tag;
		String tag = ta.getItem(position).id;
		int first = l.getFirstVisiblePosition();
		View sv = l.getChildAt(0);
		int tops = (sv==null)?0:sv.getTop();
		/** 刷新menu数据 */
		l.setAdapter(ta);
		/** 定位menu位置 */
		l.setSelectionFromTop(first, tops);
		
		if (getActivity() instanceof MainActivity) {
			MainActivity main = (MainActivity)getActivity();
			/** 改变图标 */
			main.getSupportActionBar().setIcon(ta.getItem(position).iconRes);
			/** 改变副标题 */
			main.getSubTitle().setText("-"+subTitle);
			/** 实例化中心区域内容 */
			Fragment fragment = SwitchFragmentUtil.getTopicFragment(tag);
			if (fragment == null) {
				fragment = SwitchFragmentUtil.newFragment(position);
				SwitchFragmentUtil.bindTopicFragment(tag, fragment);
			}
			/** 调用切换面板准备 */
			if(fragment != null)
				switchFragment(fragment, main);
		}
		
	}
	
	
	
}
